package nmrworkflow.framework.executionmodule;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import nmrworkflow.framework.beans.datasets.NmrDataSet;
import nmrworkflow.framework.beans.datatypes.NmrFile;
import nmrworkflow.framework.executionmodule.interfaces.DataProcessor;


abstract public class FileProcessor<I extends NmrDataSet<NmrFile>, O extends NmrDataSet<NmrFile>> implements DataProcessor<I, O> {
	
	protected Process process;
	final protected Class inputType, outputType;
	private I in;
	private O out;
	
	protected FileProcessor(Class inputType, Class outputType)
	{
		this.inputType = inputType;
		this.outputType = outputType;
	}
	
	public void run() throws IOException, InterruptedException
	{
		run(true);
	}
	
	public void run(boolean wait) throws IOException, InterruptedException
	{
		Runtime r = Runtime.getRuntime();
		String[] cmd = this.buildCommandLine(in.getData(), out.getData());
		
		process = r.exec(cmd);
		
		if(wait)
			process.wait();
	}
	
	public abstract String[] buildCommandLine(File input, File Output);
	
	public Process getProcess()
	{
		return process;
	}
	
	public InputStream getInputStream()
	{
		return process.getInputStream();
	}
	
	public OutputStream getOutputStream()
	{
		return process.getOutputStream();
	}
	
	public InputStream getErrorStream()
	{
		return process.getErrorStream();
	}
	
	public void setInput(I input)
	{
		this.in = input;
	}
	
	public void setOutput(O output)
	{
		this.out = output;
	}
	
	public Class getInputType()
	{
		return this.inputType;
	}
	
	public Class getOutputType()
	{
		return this.outputType;
	}
}
